package com.platform.tony.controller;


import com.alibaba.fastjson.JSONObject;
import com.google.code.kaptcha.Producer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;

@Slf4j
@Controller
public class IndexController {

    // https://blog.csdn.net/Ying_ph/article/details/129353108

    @Autowired
    private Producer producer;

    @GetMapping("/vc.jpg")
    public void getVerifyCode(HttpServletResponse response, HttpSession session) throws IOException {

        String code = producer.createText();
        session.setAttribute("kaptcha", code); //可以更换成 redis 实现
        BufferedImage bi = producer.createImage(code);
        response.setContentType("image/png");
        ServletOutputStream os = response.getOutputStream();
        ImageIO.write(bi, "jpg", os);
    }


    @GetMapping(value = {"home", "/"})
    public String home() {

        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        log.warn("auth is {}", JSONObject.toJSONString(auth));

        return "home";
    }

    @GetMapping(value = {"login"})
    public String login() {
        return "login";
    }

    @GetMapping(value = {"logout"})
    public String logout() {
        return "login";
    }



}
